package com.yutian.endecryption.mock.service;

import com.yutian.endecryption.aes.util.SymmetricEncoder;
import com.yutian.endecryption.config.Config;
import com.yutian.endecryption.mock.mapper.SubGroupMapper;
import com.yutian.endecryption.mock.pojo.SubGroup;

/**
 * 考官分组信息service
 *
 * @author zhang peng yang ming
 * @create 2017-08-01 10:14
 */
public class SubGroupService extends BaseService<SubGroupMapper> {
    /**
     * 根据 xl（序号）查询考官分组信息
     * @param xh
     * @return
     */
    public SubGroup retrieveSubGroup (String xh) {
        SubGroup subGroup = mapper(SubGroupMapper.class).retrieveSubGroup(xh);
        sessionClose();
        // 对应字段解密
        if (subGroup != null) {
            subGroup.setKsy(SymmetricEncoder.AESDecode(Config.KEY, subGroup.getKsy()));
            subGroup.setGlbm(SymmetricEncoder.AESDecode(Config.KEY, subGroup.getGlbm()));
            subGroup.setKcxh(SymmetricEncoder.AESDecode(Config.KEY, subGroup.getKcxh()));
        }

        return subGroup;
    }

    /**
     * 创建 考官分组信息
     * @param subGroup
     * @return
     */
    public int createSubGroup (SubGroup subGroup) {
        int r = 0; // 影响记录行数

        if (retrieveSubGroup(subGroup.getXh()) != null) {
            r = updateSubGroup(subGroup);
        } else {
            // 对应字段加密
            subGroup.setKsy(SymmetricEncoder.AESEncode(Config.KEY, subGroup.getKsy()));
            subGroup.setGlbm(SymmetricEncoder.AESEncode(Config.KEY, subGroup.getGlbm()));
            subGroup.setKcxh(SymmetricEncoder.AESEncode(Config.KEY, subGroup.getKcxh()));

            r = mapper(SubGroupMapper.class).createSubGroup(subGroup);
            sessionClose();
        }

        return r;
    }

    /**
     * 更新考官分组信息
     *
     * @param subGroup
     * @return
     */
    public int updateSubGroup (SubGroup subGroup) {
        int r = 0; // 影响记录行数
        // 对应字段加密
        subGroup.setKsy(SymmetricEncoder.AESEncode(Config.KEY, subGroup.getKsy()));
        subGroup.setGlbm(SymmetricEncoder.AESEncode(Config.KEY, subGroup.getGlbm()));
        subGroup.setKcxh(SymmetricEncoder.AESEncode(Config.KEY, subGroup.getKcxh()));

        r = mapper(SubGroupMapper.class).updateSubGroup(subGroup);
        sessionClose();

        return r;
    }
}
